import { request,requestWithAuth } from '../utils/request';

export default {

  namespace: 'lecture',

  state: {
    nowlecture:{},
    teacher:{
        name:'??',
        intro:'??',
        photot:'https://mzhuoyi-1253153042.cos.ap-chengdu.myqcloud.com/teacherpic/teacher1.jpg',
    },
    startpic:'https://mzhuoyi-1253153042.cos.ap-chengdu.myqcloud.com/classpic/class1.png',
    classname:'test',
    intro:'暂无',
    price: 22,
    rate:5,
    chapter:[
        {
            name: '第一章',
            order: 1,
            video: [
                {name: "1.1 是的但是", video_id: "test2_1_1"},
                {name: "1.2 d s", video_id: "test2_1_2"}
            ]
        },
        {
            name: '第二章',
            order: 2,
            video: [
                {name: "2.1 dssd", video_id: "test2_2_1"},
                {name: "2.2 sdsd", video_id: "test2_2_2"}
            ]
        }
    ],
    text:'你好',
    currentid:0, //当前获取的记录数
    getnum:10, //每次获取的记录数
    classlist:[],
    lectureAuth:{
        buy: 0,
        progress: 0,
        learn: 0,
    }
  },

  reducers: {
    saveSinglelecture(state, {payload:{data}}){
        return {...state, ...data}
    },
    changeAuth(state, {payload:{data}}){
        return {...state, lectureAuth:data};
    }
  },

  subscriptions: {
  },

  effects: {
    *getLecture({payload:{cid}},{put}) {
        const { data } = yield request(`http://127.0.0.1:7001/api/v1/lecture/${cid}`,{
            method: 'get',
            headers: {
                "content-type": "application/json"
            }
        });
        yield put({type:"saveSinglelecture",payload:{data}});
    },
    *getLectureR({payload:{cid}},{put}) {
        const { data } = yield requestWithAuth(`http://127.0.0.1:7001/api/v1/lectureR/${cid}`,{
            method: 'get',
            headers: {
                "content-type": "application/json"
            }
        });        
        if (data.code === 401) {
            yield put({type:'user/LoginOut'});
        } else {
            yield put({type:'changeAuth',payload:{data}});
        }
    }
  },
};
